package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.oa.domain.vo.OaFinanceEntertainVo;
import com.ruoyi.oa.domain.vo.OaFinanceTripApplyVo;
import com.ruoyi.oa.service.IOaFinanceTripApplyService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaFinanceTravel;
import com.ruoyi.oa.domain.vo.OaFinanceTravelVo;
import com.ruoyi.oa.domain.bo.OaFinanceTravelBo;
import com.ruoyi.oa.service.IOaFinanceTravelService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.vo.OaProjectVo;


/**
 * 差旅费
 *
 * @author wangxq
 * @date 2023-05-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/financeTravel")
public class OaFinanceTravelController extends BaseController {

    private final IOaFinanceTravelService iOaFinanceTravelService;

    private final IOaFinanceTripApplyService iOaFinanceTripApplyService;

    /**
     * 查询差旅费列表
     */
//    @SaCheckPermission("oa:financeTravel:list")
    @GetMapping("/list")
    public TableDataInfo<OaFinanceTravelVo> list(OaFinanceTravelBo bo, PageQuery pageQuery) {
        return iOaFinanceTravelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出差旅费列表
     */
//    @SaCheckPermission("oa:financeTravel:export")
    @Log(title = "差旅费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaFinanceTravelBo bo, HttpServletResponse response) {
        List<OaFinanceTravelVo> list = iOaFinanceTravelService.queryList(bo);
        ExcelUtil.exportExcel(list, "差旅费", OaFinanceTravelVo.class, response);
    }

    /**
     * 获取差旅费详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:financeTravel:query")
    @GetMapping("/{id}")
    public R<OaFinanceTravelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaFinanceTravelService.queryById(id));
    }

    /**
     * 新增差旅费
     */
//    @SaCheckPermission("oa:financeTravel:add")
    @Log(title = "差旅费", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaFinanceTravelBo> add(@Validated(AddGroup.class) @RequestBody OaFinanceTravelBo bo) {
        if (!StringUtils.isEmpty(bo.getProjectName())){
            OaFinanceTripApplyVo oaFinanceTripApplyVo = iOaFinanceTripApplyService.queryByProjectName(bo.getProjectName());
            if (ObjectUtil.isEmpty(oaFinanceTripApplyVo)){
                return R.fail(bo.getProjectName()+"出差申请流程状态尚未开始");
            }else {
                if (oaFinanceTripApplyVo.getActBusinessStatus().getStatus().equals("finish")){
                    if (StringUtils.isNotBlank(bo.getProjectName())) {
                        OaFinanceTravelVo oaFinanceTravelVo = iOaFinanceTravelService.queryByProjectName(bo.getProjectName());
                        if (ObjectUtil.isNotEmpty(oaFinanceTravelVo)) {
                            return R.fail(bo.getProjectName() + "已经存在,不能重复申请.");
                        }
                    }
                    return R.ok(iOaFinanceTravelService.insertByBo(bo));
                }else{
                    return R.fail(bo.getProjectName()+"出差申请流程状态尚未通过");
                }
            }
        }else {
            return R.ok(iOaFinanceTravelService.insertByBo(bo));
        }
    }

    /**
     * 修改差旅费
     */
//    @SaCheckPermission("oa:financeTravel:edit")
    @Log(title = "差旅费", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaFinanceTravel> edit(@Validated(EditGroup.class) @RequestBody OaFinanceTravelBo bo) {
        return R.ok(iOaFinanceTravelService.updateByBo(bo));
    }

    /**
     * 删除差旅费
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:financeTravel:remove")
    @Log(title = "差旅费", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaFinanceTravelService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取没有采购流程的项目信息
     *
     */
    @GetMapping("/getProjectList")
    public R<List<OaProjectVo>> getProjectList() {
        return R.ok(iOaFinanceTravelService.getProjectList());
    }


    @RequestMapping("/queryByProjectName")
    public R<OaFinanceTravelVo> queryByProjectName(@RequestBody Map<String,Object> map) {
        return R.ok(iOaFinanceTravelService.queryByProjectName(map.get("projectName").toString()));
    }
}
